package pers.agony;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.net.UnknownHostException;

/**
 * @author : Agony
 * @date : Created in 9:45 下午 2021/11/6
 * @Description: springboot的启动类
 * @Modified By:
 * @version: : 1.0
 */
//MapperScan 用于扫描指定包下的所有的接口，将接口产生代理对象交给spring容器
@MapperScan(basePackages="pers.agony.dao")
@SpringBootApplication
@EnableScheduling //开启任务的注解支持
public class MybatisApplication {
    public static void main(String[] args) {
        SpringApplication.run(MybatisApplication.class,args);
    }

     /* 第二种, 重新设置key的序列化对象
    @Resource
    public void abc(RedisTemplate redisTemplate){
        redisTemplate.setKeySerializer(new StringRedisSerializer());
    }*/

    @Bean
    public RedisTemplate<Object, Object> redisTemplate(
            RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        //设置key的值为字符串序列化方式 那么在使用过程中key 一定只能是字符串
        // 设置key,value序列化对象, 默认使用的是jdk序列化
        // 设置key的序列化方式为字符串的序列化方式string.getBytes(), 返回序列化new String(bytes)
        template.setKeySerializer(new StringRedisSerializer());
        //设置value的序列化机制为JDK自带的方式, jdk方式为默认的方式，可以使用任意类型的数据
        template.setValueSerializer(new JdkSerializationRedisSerializer());
        return template;
    }
}
